package com.example.lenovo.listvitesqlite;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

class StudentAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Student> studentArrayList;
    private IOnDataChangeListener listener;

    public StudentAdapter(Context context, ArrayList<Student> studentArrayList,IOnDataChangeListener listener) {
        this.context = context;
        this.studentArrayList = studentArrayList;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return studentArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return studentArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null){
            //设置学生列表布局
            view = LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false);

            StudentViewHolder holder = new StudentViewHolder();
            holder.txtNo = view.findViewById(R.id.stu_no);
            holder.txtName = view.findViewById(R.id.stu_name);
            holder.txtAge = view.findViewById(R.id.stu_age);
            holder.lv_del = view.findViewById(R.id.iv_del);

            view.setTag(holder);
        }

        StudentViewHolder holder = (StudentViewHolder) view.getTag();
        final Student s = studentArrayList.get(i);

        //获取学生信息显示在listView
        holder.txtNo.setText(s.getStuno());
        holder.txtName.setText(s.getName());
        holder.txtAge.setText(String.valueOf(s.getAge()));
        //删除事件
        holder.lv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.delStu(s);
            }
        });

        return view;
    }

}